<?php

/**
 * @Project NUKEVIET 3.x - GETNEWS
 * @Author ĐẶNG ĐÌNH TỨ (dlinhvan@gmail.com)
 * @Copyright (C) 2010 webdep24.com All rights reserved
 * @Createdate 10/08/2012 08:00
 */

if( ! defined( 'NV_IS_FILE_ADMIN' ) ) die( 'Stop!!!' );
$time_start = microtime( true );
global $urlimages, $urlink, $module, $folder, $global_config, $module_config;

$contents = "";
$folder = date( "Y_m" );
$do = 0;
$catids = "";
$error = "";
$data = array();
$link_arr = array();
$data1 = array();
$data2 = array();
$hauto = "vnex";
$module = $module_config[$module_name]['module'];
$mod_data = str_replace( '-', '_', $module );
$imglink = '';
$urlimages = "http://vnexpress.net";
$urlink = "http://vnexpress.net";

$url = filter_text_input( 'cat', 'post', '' );
$do = $nv_Request->get_int( 'do', 'post', 0 );
$catids = $nv_Request->get_int( 'catids', 'post', 0 );
$sourceid = $nv_Request->get_int( 'sourceid', 'post', 0 );
nguontin( $sourceid, $url );
$contents .= "<table class=\"tab1\">
      <thead>
         <tr>
            <td>
               " . $lang_module['info_vnex'] . "
            </td>
         </tr>
      </thead></table>";

$contents .= nv_get_cat( $module, $hauto, $url, $sourceid, $catids );

$contents .= xuat_html( 0 );

// lay thong tin html
$loop = 'div.folder-top';
$title_t = 'p a.link-topnews';
//$link_t = "div.cate_newsexcerpt p a";
$home_t = "div.cate_newsexcerpt p";
$img_t = "a img.img-topsubject";

if( $do )
{
	$id_block_content = array_unique( $nv_Request->get_typed_array( 'bids', 'post', 'int', array() ) );
	$getContent = new UrlGetContents( $global_config );
	$html = $getContent->get( $url );
	$html1 = str_get_html( $html );

	foreach( $html1->find( $loop ) as $e )
	{
		if( $e->find( $title_t, 0 ) ) $title = nv_unhtmlspecialchars( trim( $e->find( $title_t, 0 )->innertext ) );
		$alias = change_alias( $title );
		$data1[$alias]['title'] = $title;
		$data1[$alias]['alias'] = $alias;
		if( $e->find( $img_t, 0 ) )
		{
			$img = $e->find( $img_t, 0 )->src;
		}

		if( ! empty( $img ) )
		{
			if( nv_is_url( $img ) ) $data1[$alias]['homefile'] = $img;
			else  $data1[$alias]['homefile'] = $urlink . $img;
		}
		else  $data1[$alias]['homefile'] = "";
		if( $e->find( $title_t, 0 ) ) $link1 = $e->find( $title_t, 0 )->href;
		if( ! nv_is_url( $link1 ) ) $data1[$alias]['link'] = $urlink . $link1;
		else
		{
			unset( $data1[$alias] );
		}
	}
	sleep( 1 );
	foreach( $html1->find( 'div.folder-news' ) as $e )
	{
		if( $e->find( 'p a.link-title', 0 ) ) $title = change( $e->find( 'p a.link-title', 0 )->innertext );
		$alias = change_alias( $title );
		$data2[$alias]['title'] = $title;
		$data2[$alias]['alias'] = $alias;

		if( $e->find( 'a img.img-subject', 0 ) )
		{
			$img = $urlimages . $e->find( 'a img.img-subject', 0 )->src;
		}

		if( ! empty( $img ) )
		{
			if( nv_is_url( $img ) ) $data2[$alias]['homefile'] = $img;
			else  $data2[$alias]['homefile'] = $urlink . $img;
		}
		else  $data2[$alias]['homefile'] = "";
		if( $e->find( 'p a.link-title', 0 ) ) $link2 = $e->find( 'p a.link-title', 0 )->href;
		if( ! nv_is_url( $link2 ) ) $data2[$alias]['link'] = $urlink . $link2;
		else
		{
			unset( $data2[$alias] );
		}
	}
	$html1->clear();
	unset( $html1, $html );
	$data = array_merge( $data1, $data2 );

	$contents .= xuat_html( 1 ); // xuat thanh  noi dung tieu de
	$ii = 1;
	foreach( $data as $alias => $array )
	{

		$sql = "SELECT COUNT(*) FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_rows` WHERE `alias`=" . $db->dbescape_string( $array['alias'] );
		$result = $db->sql_query( $sql );
		list( $exist ) = $db->sql_fetchrow( $result );
		if( empty( $exist ) )
		{
			$html = $getContent->get( $array['link'] );
			$html1 = str_get_html( nv_convert( $html ) );
			if( ! ( $html1->find( 'div.content div', 0 ) ) )
			{
				$error .= "Dữ liệu nguồn bị thay đổi";
				$link_arr[] = $array['link'];
			}
			else
			{
				$publtime = NV_CURRENTTIME;

				$hometext = $html1->find( 'h2.Lead', 0 )->innertext;
				$hometext = preg_replace( '@<a[^>]*?.*?</a>@siu', ' ', strip_tags( $hometext ) );
				$array['hometext'] = preg_replace( '/[<>\/]/', '', $hometext );
				$html1->find( 'p[class="Normal" align="left"]', 0 )->outertext = "";
				if( $html1->find( 'div[id="flashContent"]', 0 ) ) $html1->find( 'div[id="flashContent"]', 0 )->outertext = "";
				$html1->find( 'h1.Title', 0 )->outertext = "";
				$html1->find( 'p.nomal[align="right"]', 0 )->innertext = "";
				$html1->find( 'h2.Lead', 0 )->outertext = "";
				$html1->find( 'div[class="boxnew"]', 0 )->outertext = "";
				$body = $html1->find( 'div.content div', 0 )->innertext;
				$bodytext = preg_replace( "'<script[^>]*>.*</script>'siU", '', $body );
				$bodytext = url_link( $bodytext, $urlink );
				if( $module_config['getnews']['load_image'] == 1 )
				{
					$bodytext = load_image( $bodytext, '' );
				}
				$bodytext = stripwhitespace( $bodytext );

				if( ! empty( $bodytext ) )
				{
					$imgfile = nv_get_images( $bodytext );
					if( ! empty( $imgfile ) and $module_config[$module_name]['image_body'] == 1 )
					{
						$img = explode( "/", $imgfile );
						$imgfile = end( $img );
						$imgfile = NV_UPLOADS_REAL_DIR . "/" . $module . "/" . $folder . "/" . $imgfile;
						if( file_exists( $imgfile ) )
						{
							$img_file = nv_filter( $imgfile );
							$homefile = $img_file[0];
							$homeimgthumb = $img_file[1];
						}
						elseif( ! empty( $array['homefile'] ) )
						{
							$homefile = $array['homefile'];
							$homeimgthumb = "";
						}
						elseif( ! empty( $imgfile ) )
						{
							$homefile = "";
							$homeimgthumb = "";
						}
					}
					elseif( $array['homefile'] != "" )
					{
						$homefile = $array['homefile'];
						$homeimgthumb = "";

					}
					elseif( ! empty( $imgfile ) )
					{
						$homefile = $imgfile;
						$homeimgthumb = "";
					}
					else
					{
						$homefile = "";
						$homeimgthumb = "";
					}

				}
				else
				{
					$error .= "Lỗi chi tiết bài viết trống";
					break;
				}

				if( empty( $error ) )
				{
					nv_save_data( $module, $catids, $sourceid, $array['title'], $array['alias'], $array['hometext'], $publtime, $homefile, $homeimgthumb, $bodytext, $array['link'], $id_block_content );

					$contents .= "<table width=\"100%\" border=\"1\">";
					$contents .= "  
						<tr>
							<td width='5%' style=\"height:26px;\" align=center><strong>$ii</strong></td>
							<td width='61%'><strong><a style=\"padding-left:10px;text-decoration:none;color:black\" href=\"" . $array['link'] . "\" target='_blank'>" . $array['title'] . "<img src=\"" . NV_BASE_SITEURL . "themes/default/images/" . $module_name . "/newnew.gif\"></a></strong></td>
							<td width='34%' align=center style=\"width:100px;\"><strong>" . date( 'H:i, d-m-Y', $publtime ) . "</strong></td>
						</tr>";
					$contents .= "</table>";
				}
				$html1->clear();
				unset( $html1, $html, $bodytext );

			}

		}
		$ii++;
		if( $ii > 10 ) break;
	}
	if( ! empty( $error ) )
	{
		nv_sendmail( array( $global_config['site_name'], $global_config['site_email'] ), $global_config['error_send_email'], "Error get new :" . $error, "Error get new :" . $error . " từ website " . NV_SERVER_NAME . " nguồn " . $url . "  " . implode( "<br />", $link_arr ) . "  " . $global_config['site_email'] );
	}
	else
	{
		optimize();
		nv_del_moduleCache( $module );
	}

}
$time_end = microtime( true );
$time = $time_end - $time_start;

$contents .= "<br/>" . $error . "<br/>\n";
$contents .= "<br/>Thời gian tải tin: " . $time . " giây\n";
include ( NV_ROOTDIR . "/includes/header.php" );
echo nv_admin_theme( $contents );
include ( NV_ROOTDIR . "/includes/footer.php" );

?>